#include "ListenAndSpeak.h"
#include "PcmToText.h"
#include "TextToPcm.h"

using namespace std;

// 这里我只把用到gio库的部分放在了这里
// 省下去专门写一个桥

static PcmToText *pcmToText = nullptr;
static TextToPcm *textToPcm = nullptr;

ListenAndSpeak::ListenAndSpeak()
{
    pcmToText = new PcmToText(); // 初始化 PcmToText 对象
    textToPcm = new TextToPcm(); // 初始化 TextToPcm 对象
    cout << "---ListenAndSpeak initialized---\n";
}

ListenAndSpeak::~ListenAndSpeak()
{
    delete pcmToText;

    pcmToText = nullptr; // 清除指针

    delete textToPcm;

    textToPcm = nullptr; // 清除指针

    cout << "---ListenAndSpeak destroyed---\n";
}

string ListenAndSpeak::receive_in() {

    string output_text = "";
    cout << "---录音开始---\n";

    string end_message0 = outputAndReceive.receive_voice();
    cout << end_message0 << endl;

    cout << "---录音结束---\n";

    cout << "---开始pcm文件识别---\n";
    output_text =  pcmToText->get_model_response(); // pcm文件识别
    cout << "---pcm 转 text 成功---\n";
    cout << output_text << endl;

    return output_text;
}

string ListenAndSpeak::speak_out(string respond) {
    cout << "---开始转语音---\n";
    string output_message1 = textToPcm->get_model_response(respond); // 转语音
    cout << "---文字转语音成功---\n";
    // cout << output_message1 << endl;

    
    cout << "---开始播放语音---\n";

    outputAndReceive.output_voice(); // 播放语音

    cout << "---语音播放结束---\n";

    return "---speak out is at the end---";
}